package com.redisdemo.controller;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.redisdemo.domain.Score;
import com.redisdemo.utlis.CryptoUtils;
import com.redisdemo.utlis.JsonUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <h3>redisDemo</h3>
 * <p>加密通信控制类</p>
 *
 * @author gdw
 * @since 2024-06-27 13:50:02
 */
@RestController
@RequestMapping("/cryto")
public class CryptoController {
    private static final String SECRET_KEY="1902765sdfnxjkl8";

    /**
     * 生成加密数据
     * @return
     */
    @GetMapping("/generate")
    public String generate() {
        String date="Hell, Project B!";
        String emcryptDate;
        try {
            emcryptDate= CryptoUtils.encrypt(date,SECRET_KEY);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return  emcryptDate;
    }

    /**
     * 解析数据
     * @param shu
     * @return
     */
    @GetMapping("/analysis")
    public String analysis(@RequestParam String shu) {
        String decrypt;
        try {
            decrypt = CryptoUtils.decrypt(shu, SECRET_KEY);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return decrypt;
    }
    /**
     * 生成加密数据
     * @return
     */
    @GetMapping("/generateObj")
    public String generateObj() {
        Score student = Score.builder().name("小梁").score(98.0).build();
        String emcryptDate;
        try {
            emcryptDate= CryptoUtils.encryptObject(student,SECRET_KEY);
            System.out.println(emcryptDate);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return emcryptDate;
    }

    /**
     * 解析数据
     * @param shu
     * @return
     */
    @GetMapping("/analysisObj")
    public Score analysisObj(@RequestParam String shu) {
        Score score;
        try {
            score = (Score) CryptoUtils.decryptObject(shu, SECRET_KEY);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return score;
    }
    /**
     * 生成加密数据
     * @return
     */
    @GetMapping("/generateList")
    public String generateList() {
        Score student = Score.builder().name("小梁").score(98.0).build();
        Score student1 = Score.builder().name("小古").score(98.0).build();
        Score student2 = Score.builder().name("小花").score(98.0).build();
        List<Object> list=new ArrayList<>();
        list.add(student);
        list.add(student1);
        list.add(student2);
        String emcryptDate;
        try {
            emcryptDate= CryptoUtils.encryptList(list,SECRET_KEY);
            System.out.println(emcryptDate);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return emcryptDate;
    }

    /**
     * 解析数据
     * @param shu
     * @return
     */
    @PostMapping("/analysisList")
    public List<Object> analysisList(@RequestBody String shu) {
        List<Object> score=new ArrayList<>();
        try {
            String date= JsonUtils.extractShuValue(shu,"shu");
            score = CryptoUtils.decryptList(date, SECRET_KEY);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return score;
    }


}
